package it.xiuxian.personcenter.activity

import android.content.Intent
import android.net.Uri
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import it.xiuxian.lib.base.App
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseActivity
import it.xiuxian.lib.utils.DensityUtil
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector
import it.xiuxian.personcenter.R
import it.xiuxian.personcenter.bean.*
import it.xiuxian.personcenter.databinding.ActivityMyGongyouBinding
import it.xiuxian.personcenter.presenter.MyTeamActivityPresenter
import java.util.*
import kotlin.collections.ArrayList

@Route(path = ArouterAddress.MYTEAMACTIVITY)
class MyTeamActivity:BaseActivity<MyTeamActivityPresenter>() {
    @JvmField
    @Autowired
    var erweima:String?=null
    @JvmField
    @Autowired
    var yaoqingma:String?=null
    var adapter: SlimAdapter?=null
   lateinit var binding:ActivityMyGongyouBinding
    override val layoutId: Int
        get() = R.layout.activity_my_gongyou
    override val presenter: MyTeamActivityPresenter
        get() = MyTeamActivityPresenter()

    override fun initView() {
        setTitle("我的工友")
         ARouter.getInstance().inject(this)
          binding = getViewDataBinding<ActivityMyGongyouBinding>()
        (binding.include.re.layoutParams as LinearLayout.LayoutParams).setMargins(0,
                DensityUtil.getStatusPix(App.INSTANCE.applicationContext) + 5, 0, 0)
        binding.rv.layoutManager = LinearLayoutManager(this)
        adapter = SlimAdapter.create().register<Team>(R.layout.activity_my_team_item,
                object : SlimInjector<Team> {
                    override fun onInject(data: Team?, injector: IViewInjector<out IViewInjector<*>>?) {
                        injector!!.text(R.id.tv_name, data!!.nickname)
                                .text(R.id.tv_phone, data.userphone)
                                .text(R.id.tv_date, data.createdate)
                                .clicked(R.id.tv_phone, View.OnClickListener {
                                    callPhone(data.userphone)
                                })
                    }

                })
            .attachTo(binding.rv)

    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    fun callPhone(phoneNum: String) {
        val intent = Intent(Intent.ACTION_DIAL)
        val data: Uri = Uri.parse("tel:$phoneNum")
        intent.setData(data)
        startActivity(intent)
    }
    override fun initData() {
    mPresenter!!.getTeam("1")
    }
    var i=1
    override fun initEvent() {
        binding.smart.setOnRefreshListener {
            mPresenter!!.getTeam("1")
            binding.smart.finishRefresh()
        }
        binding.smart.setOnLoadMoreListener {
            mPresenter!!.getTeam(i++.toString())
        }
        binding.tvYaoqing.setOnClickListener { ARouter.getInstance().build(ArouterAddress.INVITEFRIENDSACTIVITY).withString("erweima", erweima).withString("yaoqingma", yaoqingma).navigation() }

    }

    var newList = ArrayList<Team>()
    fun setData(data: MyTeamBean) {
        binding.tvNum.text = data.num
        if(data.list.isNotEmpty()){
            if(i==1){
                newList.clear()
                newList.addAll(data.list)
                adapter!!.updateData(newList)
            }else if(i>1&&data.next!=-1){
                newList.addAll(data.list)
                adapter!!.updateData(newList)
            }
        }
        binding.smart.finishLoadMore()
    }
}